﻿Namespace PL.MESSDK
    Public Class QualityGrade_PL
        Inherits PLBase
        Private _aQueryName As String

        Public Sub New(ByVal queryName As String)
            MyBase.New(queryName)
            _aQueryName = queryName
        End Sub

        Public Overrides Function Delete() As Boolean
            Return True
        End Function

        'Public Overrides Function InitData(Optional withChild As Boolean = True) As Boolean
        '    Dim metaDataTableChild As CommonExt.MetaDataTable
        '    Dim aChildRelationField As String
        '    Dim parentGuid As Guid = Guid.Empty
        '    Try
        '        'Ici on fait nos New a cause des Reload dans la grille.
        '        Me.DataSet = New DataSet()
        '        MetaDataTable = New MetaDataTable()
        '        Me.MetaDataTable.QueryName = Me.QueryName
        '        Me.MetaDataTable.FillBusinessApplicationData()
        '        Me.DataSet.Tables.Add(Me.MetaDataTable)
        '        If withChild Then
        '            Dim aParentRelationField As String = Me.MetaDataTable.MetaCommand.GUIDField
        '            For Each row As DataRow In Me.MetaDataTable.MetaCommand.GetChildDisplayQuery.Rows
        '                metaDataTableChild = New CommonExt.MetaDataTable() With {.QueryName = row("QueryName").ToString}
        '                aChildRelationField = Me.MetaDataTable.MetaCommand.GUIDField & "_FK"
        '                metaDataTableChild.FillBusinessApplicationData()
        '                Me.DataSet.Tables.Add(metaDataTableChild)
        '                Dim aDR As New DataRelation(row("Relation").ToString, Me.MetaDataTable.Columns(aParentRelationField), metaDataTableChild.Columns(aChildRelationField))
        '                Me.DataSet.Relations.Add(aDR)
        '            Next
        '        End If
        '        Return True
        '    Catch ex As Exception
        '        Throw ex
        '    End Try
        '    Return False
        'End Function

        Public Overrides Function Save() As Boolean
            Try
                Dim table As MetaDataTable
                Dim tableRelation As MetaDataTable
                'TestPoint: Voir MetaDataTable pour les BuildCUDCommands, AssignAdapterCommandForCUD
                Dim DelDataSet As DataSet
                Dim AddDataSet As DataSet

                If Me.DataSet.HasChanges Then
                    DelDataSet = Me.DataSet.GetChanges(DataRowState.Deleted)
                    AddDataSet = Me.DataSet.GetChanges(DataRowState.Added Or DataRowState.Modified)
                    If DelDataSet IsNot Nothing Then
                        For Each table In Me.DataSet.Tables
                            '1- applychanges to child Tables only for Deleted and Updated Rows  
                            For Each relation As DataRelation In table.ChildRelations
                                tableRelation = CType(relation.ChildTable, MetaDataTable)
                                tableRelation.ApplyChanges(False, DelDataSet.Tables(relation.ChildTable.TableName))
                            Next
                            '2- Apply Changes on Parent tables
                            table.ApplyChanges(False)
                        Next
                    End If

                    'Save Added Record
                    If AddDataSet IsNot Nothing Then
                        For Each table In Me.DataSet.Tables
                            '1- Save Parent 
                            table.ApplyChanges(False, AddDataSet.Tables(table.TableName))
                            For Each relation As DataRelation In table.ChildRelations
                                '2- Save Children
                                tableRelation = CType(relation.ChildTable, MetaDataTable)
                                tableRelation.ApplyChanges(False, AddDataSet.Tables(relation.ChildTable.TableName))
                            Next
                        Next
                    End If
                    Me.DataSet.AcceptChanges()
                End If
                Return True
                'Next
            Catch sqlex As System.Data.SqlClient.SqlException
                'HACK: On Utilise les codes de SQL. Attention si les codes change entre ADO et/ou SQL
                Me.DataSet.RejectChanges()
                Select Case sqlex.Number
                    Case 2601, 2627  'Unique Index/Constriant Violation
                        Throw New Exception("Record Already Exist, Operation Aborded", sqlex)
                    Case Else
                        Throw New Exception(String.Format("Database Error: {0} #{1} ", sqlex.Message, sqlex.Number), sqlex)
                End Select
            Catch dbcex As System.Data.DBConcurrencyException
                Me.DataSet.RejectChanges()
                Throw New Exception("Concurrency Issue, Record Already Modified", dbcex)
            Catch ex As Exception
                Me.DataSet.RejectChanges()
                Throw New Exception(String.Format("Row Cannot Be Save: {0}. Make Sure You Have a CUD Command", ex.Message), ex)
            End Try
            Return False
        End Function

        Public Overrides Function Validate() As Boolean
            Return True
        End Function
    End Class
End Namespace
